package priv.ln.team.service;

import priv.ln.team.domain.*;

/**
 * ClassName: NameListService
 * Package: priv.ln.team.service
 * Description:
 * 将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法
 *
 * @Author 辣辣
 * @Create 2025/4/17 17:36
 * @Version 1.0
 */
public class NameListService {
    private Employee[] employees;

    public NameListService() {
        employees = new Employee[Data.EMPLOYEES.length];
        for (int i = 0; i < Data.EMPLOYEES.length; i++) {
            int num = Integer.parseInt(Data.EMPLOYEES[i][0]);
            int id = Integer.parseInt(Data.EMPLOYEES[i][1]);
            String name = Data.EMPLOYEES[i][2];
            int age = Integer.parseInt(Data.EMPLOYEES[i][3]);
            double salary = Double.parseDouble(Data.EMPLOYEES[i][4]);
            Equipment equipment;
            double bonus;
            int stock;
            switch (num) {
                case Data.EMPLOYEE:
                    employees[i] = new Employee(id, name, age, salary);
                    break;
                case Data.PROGRAMMER:
                    if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PC) {
                        equipment = new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
                    } else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.NOTEBOOK) {
                        equipment = new NoteBook(Data.EQUIPMENTS[i][1], Double.parseDouble(Data.EQUIPMENTS[i][2]));
                    } else {
                        equipment = new Printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
                    }
                    employees[i] = new Programmer(id, name, age, salary, equipment);
                    break;
                case Data.DESIGNER:
                    if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PC) {
                        equipment = new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
                    } else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.NOTEBOOK) {
                        equipment = new NoteBook(Data.EQUIPMENTS[i][1], Double.parseDouble(Data.EQUIPMENTS[i][2]));
                    } else {
                        equipment = new Printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
                    }
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    employees[i] = new Designer(id, name, age, salary, equipment, bonus);
                    break;
                case Data.ARCHITECT:
                    if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.PC) {
                        equipment = new PC(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
                    } else if (Integer.parseInt(Data.EQUIPMENTS[i][0]) == Data.NOTEBOOK) {
                        equipment = new NoteBook(Data.EQUIPMENTS[i][1], Double.parseDouble(Data.EQUIPMENTS[i][2]));
                    } else {
                        equipment = new Printer(Data.EQUIPMENTS[i][1], Data.EQUIPMENTS[i][2]);
                    }
                    bonus = Double.parseDouble(Data.EMPLOYEES[i][5]);
                    stock = Integer.parseInt(Data.EMPLOYEES[i][6]);
                    employees[i] = new Architect(id, name, age, salary, equipment, bonus, stock);
                    break;
            }
        }

    }

    public Employee[] getAllEmployees() {
        return employees;
    }

    public Employee getEmployee(int id) throws TeamException{
        for (int i = 0; i < employees.length; i++) {
            if (employees[i].getId() == id) {
                return employees[i];
            }
        }
        throw new TeamException("找不到指定的员工");
    }



}
